#include "comAPI.h"

FrameAPI gl_CurrentFrameAPI;

void makeATFrame(unsigned char* ATCmd, unsigned char* parameter, unsigned char frameID)
{
    int iCpt;

    // identifieur de commande API = Commande AT (0x80)
    gl_CurrentFrameAPI.content.frame.frameType = API_CMD_AT_CMD;
    // ajout de l'identifiant pass� en param�tre
    gl_CurrentFrameAPI.content.frame.api.atRequest.frameID = frameID;

    // ajout de la commande AT ex : "NI"
    for(iCpt = 0; iCpt < 2 ; iCpt ++)
    {
        gl_CurrentFrameAPI.content.frame.api.atRequest.ATCommand[iCpt] = ATCmd[iCpt];
    }

    iCpt = 0;
    // ajout des param�tres de la commande AT (si il y en a).
    while(parameter[iCpt] != '\0')
    {
        gl_CurrentFrameAPI.content.frame.api.atRequest.Parameter[iCpt] = parameter[iCpt];
        iCpt ++;
    }
    iCpt += 4;
    // ajout des champs longueur de trame.
    gl_CurrentFrameAPI.length_MSB = iCpt / 256;
    gl_CurrentFrameAPI.length_LSB = iCpt % 256;

    // calcul de la checksum.
    gl_CurrentFrameAPI.content.frame.checksum = Checksum();
}

void makeDataTransmitFrame(unsigned char* data, int dataLen, unsigned char dest_H[4], unsigned char dest_L[4], unsigned char dest_16bits[2])
{
    int iCpt;

    // identifieur de commande API = Request Transmit (0x10)
    gl_CurrentFrameAPI.content.frame.frameType = API_CMD_TX_REQUEST;
    // ajout d'un frameID valant 0x01
    gl_CurrentFrameAPI.content.frame.api.transmitRequest.frameID = 0x01;

    // adresse de destination sur 64 bit
    for(iCpt = 0 ; iCpt < 4 ; iCpt ++)
    {
        gl_CurrentFrameAPI.content.frame.api.transmitRequest.destAdd64H[iCpt] = dest_H[iCpt];
        gl_CurrentFrameAPI.content.frame.api.transmitRequest.destAdd64L[iCpt] = dest_L[iCpt];
    }

    // adresse r�seau de destination sur 16 bit.
    gl_CurrentFrameAPI.content.frame.api.transmitRequest.destAddNet[0] = dest_16bits[0];
    gl_CurrentFrameAPI.content.frame.api.transmitRequest.destAddNet[1] = dest_16bits[1];

    // ajout du champ rayon de broadcast et options.
    gl_CurrentFrameAPI.content.frame.api.transmitRequest.broadcastRadius = 0x00;
    gl_CurrentFrameAPI.content.frame.api.transmitRequest.options = 0x00;

    for(iCpt = 0 ; iCpt < dataLen ; iCpt ++) // ajout des donn�es � la trame
    {
        gl_CurrentFrameAPI.content.frame.api.transmitRequest.dataToSend[iCpt] = data[iCpt];
    }
    iCpt += 14;
    // ajout des champs longueur de trame.
    gl_CurrentFrameAPI.length_MSB = 0;
    gl_CurrentFrameAPI.length_LSB = iCpt;

    // calcul de la checksum.
    gl_CurrentFrameAPI.content.frame.checksum = Checksum();
}

void sendAPI(void)
{
    int iCpt;
    char curCar;
    int FrameSize = gl_CurrentFrameAPI.length_MSB*256 + gl_CurrentFrameAPI.length_LSB;

    Emission1CarUSART1(FRAME_DELIMITER);
    Emission1CarUSART1(gl_CurrentFrameAPI.length_MSB);
    Emission1CarUSART1(gl_CurrentFrameAPI.length_LSB);
    for (iCpt = 0 ; iCpt < FrameSize ; iCpt ++)
    {
        curCar = gl_CurrentFrameAPI.content.data[iCpt];
        // si on a un caract�re � echapper alors on transmet le caract�re d'�chappement et le caractere � transmetre XOR 0x20
        if((curCar == FRAME_DELIMITER) || (curCar == ESCAPE) || (curCar == XON) || (curCar == XOFF))
        {
            Emission1CarUSART1(ESCAPE);
            Emission1CarUSART1(curCar^0x20);
        }
        else Emission1CarUSART1(curCar);
    }
    Emission1CarUSART1(gl_CurrentFrameAPI.content.frame.checksum);
}

unsigned char Checksum(void)
{
    int checksum = 0;
    int DataSize = gl_CurrentFrameAPI.length_MSB*256 + gl_CurrentFrameAPI.length_LSB;
    int iCpt = 0;
    for(iCpt = 0 ; iCpt < DataSize ; iCpt ++)
    {
        checksum += gl_CurrentFrameAPI.content.data[iCpt];
    }
    return (unsigned char)(0xFF - ( checksum & 0xFF ));
}
